package com.flower.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.flower.mapper.ICartItemDao;
import com.flower.pojo.CartItem;
import com.flower.pojo.Product;
import com.flower.pojo.User;
import com.flower.service.ICartService;

@Service("cartService")
public class CartServiceImpl implements ICartService{
	
	@Resource(name = "ICartItemDao")
	private ICartItemDao iCartItemDao;
	@Override
	public List<CartItem> getAllCartItem(int uid) {
		return iCartItemDao.getAllCartItem(uid);
	}
	
	@Override
	public CartItem getCartItem(int uid, int pid) {
		return iCartItemDao.getCartItem(uid, pid);
	}
	
	@Transactional
	@Override
	public CartItem addCart(int num, CartItem cartItem, Product product, User user){
		//创建购物项
		cartItem = new CartItem();
		//封装商品
		cartItem.setProduct(product);
		cartItem.setUid(user.getUid());
		cartItem.setSubTotal(product.getMarket_price() * cartItem.getPnum());
		cartItem.setPnum(num);
		cartItem.setSubTotal(num * product.getShop_price());
		
		//将购物项添加进购物车
		iCartItemDao.saveCartItem(cartItem);
		
		return cartItem;
			
	}

	@Transactional
	@Override
	public int updateCartItem(int pnum, double subTotal, int uid, int pid) {
		return iCartItemDao.updateCartItem(pnum, subTotal, uid, pid);
	}

	@Transactional
	@Override
	public int deleteCartItem(int uid, int pid) {
		return iCartItemDao.deleteCartItem(uid, pid);
	}

	@Transactional
	@Override
	public int clearCart(int uid) {
		return iCartItemDao.clearCart(uid);
	}

}
